{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "961ae752",
   "metadata": {},
   "source": [
    "## 上周回顾\n",
    "> * 1.list的一般方法/函数的使用及案例，如：\n",
    ">> 1. append() 增加，增加list元素到list的末尾，常用于新建列表\n",
    ">> 2. pop(index)/remove(values)对比两者的优势\n",
    ">> 3. count()查看某个元素出现的次数\n",
    ">> 4. index()查看某个元素第一次出现的位置\n",
    ">> 5. extend() 两个list合并在一起，也可以用 []+[]来实现\n",
    ">> 6. sort() list元素的排序\n",
    "\n",
    "> * 2. list 的高级用法；如\n",
    ">> 1.copy() list(可变的数据类型）复制 不等价于 赋值，可用[:]来实现\n",
    ">> 2.enumerate() 枚举，可同时遍历并获取index和values\n",
    "## 本周内容\n",
    "### list练习，vowels.py vowels2.py\n",
    "### list实践：\n",
    "### 初始字典：\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f509788b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hhhhh'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name = \"moxiufang\"\n",
    "name = \"hhhhh\"\n",
    "name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c3df590",
   "metadata": {},
   "outputs": [],
   "source": [
    "> * "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0fcd613d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "i\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "vowels =['a','e','i','o','u']\n",
    "word = \"Milliways\"\n",
    "# 检查 word中出现的 元音，并打印出来 怎么做？\n",
    "# for letter in word:   #   第一个元素letter 请不要和已有变量重名   ，（也可以写i 或item其他的值代替它）     (这句话很重要，要记下来)\n",
    "                      #    第一个元素一般不定义名字，常用item(元素)来命名                               （这些for循环不懂的就好好地看这里了）\n",
    "                      #                              简写 i 来命名\n",
    "                       # 第二个元素word （重要），意味着将遍历的内容 (遍历的对象是在第二个元素出现的)\n",
    "                       # 遍历的内容一般是 序列数据 str list\n",
    "for letter in word:\n",
    "#     print(letter)\n",
    "    if letter in vowels:\n",
    "        print(letter)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c044350",
   "metadata": {},
   "source": [
    " > * vowels2.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "043f5661",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['i', 'a']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用append() 新建列表\n",
    "found = []\n",
    "for letter in word:\n",
    "#     print(letter)\n",
    "    if letter in vowels: # letter 在 元音字母表中\n",
    "        if letter not in found: #letter 不在found表中 \n",
    "            found.append(letter)\n",
    "        \n",
    "        \n",
    "found        "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b9dc8b3",
   "metadata": {},
   "source": [
    "只要是重复出现的事情，什么都不用去想，就用for循环就对了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1c79ccc8",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "6b1660f5",
   "metadata": {},
   "source": [
    "## list实践：\n",
    "    购物车程序项目：\n",
    "    要求：\n",
    "    1.运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "    2. 让用户输入商品编号进行商品的购买\n",
    "    3. 用户选择后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。(用append增加）（循环有两种方式，for循环和will true，看你用哪种）\n",
    "    4.用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14f88ffb",
   "metadata": {},
   "outputs": [],
   "source": [
    "banlce = int(input(\"请输入您的支付宝余额: \"))\n",
    "\n",
    "shopping_car = [] # 购物车\n",
    "goods = ([\"1\", \"xpxvill\", \"￥\", 2599],\n",
    "         [\"2\", \"kith\", \"￥\", 1099],\n",
    "         [\"3\", \"ambush\", \"￥\", 1599])\n",
    "info = '''\n",
    "----------items----------\n",
    "1. xpxvill   ￥ 599\n",
    "2. kith  ￥ 1099\n",
    "3. ambush   ￥ 1599\n",
    "-------------------------\n",
    "'''\n",
    "print(info)\n",
    "while True:\n",
    "    shopindex = int(input(\"请选择你要购买的商品并输入商品编号: \"))\n",
    "    if banlce > goods[shopindex - 1][3]:\n",
    "        shopping_car.append(goods[shopindex - 1])\n",
    "        banlce -= int(goods[shopindex - 1][3])\n",
    "        print(\"恭喜你！你成功购买了 {name} !\".format(name=goods[shopindex - 1][1]))\n",
    "        print(\"你当前支付宝的余额: ￥\", banlce)\n",
    "        decision = input(\"你还有其他选择吗？\")\n",
    "        print('退出请按[2]')\n",
    "        choice = input('2')\n",
    "        if choice == '2':\n",
    "            print(shopping_car)\n",
    "            print('感谢使用，欢迎下次再来！')\n",
    "    else:\n",
    "        print(\"你当前支付宝的余额不足!请尝试其他选择.\")\n",
    "        recharge_ans = input(\"你想要进行支付宝余额充值吗？\")\n",
    "        if recharge_ans == \"是的\":\n",
    "            recharge = int(input(\"请输入充值的金额: \"))\n",
    "            print(\"请等待...\")\n",
    "            banlce += recharge\n",
    "            print(\"恭喜你！余额充值成功！\")\n",
    "            print(\"你现在的支付宝余额为: \", banlce, \"当前！\")\n",
    "        decision = input(\"你还有其他选择吗？\")\n",
    "        print('退出请按[2]')\n",
    "        choice = input('2')\n",
    "        if choice == '2':\n",
    "            print(shopping_car)\n",
    "            print('感谢使用，欢迎下次再来！')\n",
    "    if decision == \"q\":\n",
    "        break\n",
    "    else:\n",
    "        continue\n",
    "print(\"你已成功购买的商品: \", shopping_car)\n",
    "print(\"你当前支付宝余额: ￥\", banlce)\n",
    "print(\"欢迎下次再来!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d2477dfa",
   "metadata": {},
   "source": [
    "## 字典一般方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f50e367d",
   "metadata": {},
   "source": [
    "字典的循环遍历\n",
    "\n",
    "fromkeys()方法\n",
    "\n",
    "setdefault()方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "546e8051",
   "metadata": {},
   "outputs": [],
   "source": [
    "info = {\n",
    "    \"stu1801\":\"ZiQi\",\n",
    "    \"stu1802\":\"XiaoFeng\",\n",
    "    \"stu1803\":\"LiuYu\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5aadf39b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'ZiQi', 'stu1802': 'XiaoFeng', 'stu1803': 'LiuYu'}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6eef7d55",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1801 ZiQi\n",
      "1802 XiaoFeng\n",
      "1803 LiuYu\n"
     ]
    }
   ],
   "source": [
    "# 循环遍历字典\n",
    "# 循环遍历的最大用途就是：给所有的值使用同样的 操作/方法\n",
    "stu_id_list = []\n",
    "for k,v in info.items():\n",
    "    print(k[3:],v)\n",
    "    stu_id_list.append(k[3:])\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e896a03b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['1801', '1802', '1803']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_id_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43eca169",
   "metadata": {},
   "source": [
    "## fromkeys()方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "88ffa207",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function fromkeys:\n",
      "\n",
      "fromkeys(iterable, value=None, /) method of builtins.type instance\n",
      "    Create a new dictionary with keys from iterable and values set to value.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(info.fromkeys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c1a25b37",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stud2101': 123456, 'stud2102': 123456, 'stud2103': 123456}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 初始化统一值的字典信息\n",
    "学生初始密码={}\n",
    "学生初始密码.fromkeys([\"stud2101\",\"stud2102\",\"stud2103\"],123456)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a9d1ed6b",
   "metadata": {},
   "source": [
    "setdefault()方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e91f5d1e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function setdefault:\n",
      "\n",
      "setdefault(key, default=None, /) method of builtins.dict instance\n",
      "    Insert key with a value of default if key is not in the dictionary.\n",
      "    \n",
      "    Return the value for key if key is in the dictionary, else default.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(info.setdefault)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6ae4cea4",
   "metadata": {},
   "outputs": [],
   "source": [
    "info.setdefault('stu1804')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "28fe351b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'zhichao'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info.setdefault('stu1805','zhichao')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "cd90477e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'ZiQi',\n",
       " 'stu1802': 'XiaoFeng',\n",
       " 'stu1803': 'LiuYu',\n",
       " 'stu1804': None,\n",
       " 'stu1805': 'zhichao'}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info # none为默认键值v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "c5982e20",
   "metadata": {},
   "outputs": [],
   "source": [
    "dict = {'runoob': '菜鸟教程', 'google': 'Google 搜索'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "73726f71",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'菜鸟教程'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict.setdefault('runoob')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "cd2557de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'runoob': '菜鸟教程', 'google': 'Google 搜索'}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "8c433672",
   "metadata": {},
   "outputs": [],
   "source": [
    "dict.setdefault('iron man')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "6d577af9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'runoob': '菜鸟教程', 'google': 'Google 搜索', 'iron man': None}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "6db56556",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'chuige'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict.setdefault('thor','chuige')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "70a61de0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'runoob': '菜鸟教程', 'google': 'Google 搜索', 'iron man': None, 'thor': 'chuige'}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0c41fe5d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5625e71",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "acbeff8f",
   "metadata": {},
   "source": [
    "# 我的另外笔记：\n",
    "Python列表操作的函数和方法\n",
    "列表操作包含以下函数:\n",
    "1、cmp(list1, list2)：比较两个列表的元素 \n",
    "2、len(list)：列表元素个数 \n",
    "3、max(list)：返回列表元素最大值 \n",
    "4、min(list)：返回列表元素最小值 \n",
    "5、list(seq)：将元组转换为列表 \n",
    "列表操作包含以下方法:\n",
    "1、list.append(obj)：在列表末尾添加新的对象\n",
    "2、list.count(obj)：统计某个元素在列表中出现的次数\n",
    "3、list.extend(seq)：在列表末尾一次性追加另一个序列中的多个值（用新列表扩展原来的列表）\n",
    "4、list.index(obj)：从列表中找出某个值第一个匹配项的索引位置\n",
    "5、list.insert(index, obj)：将对象插入列表\n",
    "6、list.pop(obj=list[-1])：移除列表中的一个元素（默认最后一个元素），并且返回该元素的值\n",
    "7、list.remove(obj)：移除列表中某个值的第一个匹配项\n",
    "8、list.reverse()：反向列表中元素\n",
    "9、list.sort([func])：对原列表进行排序"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0cdb1f3d",
   "metadata": {},
   "source": [
    " 字典是可变的数据结构（增删改查）\n",
    "* 字典删除的关键词也是唯一的\n",
    "* pop\n",
    "* del"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
